package com.jingfu.controller;

import com.jingfu.Service.RoleService;
import com.jingfu.Service.UserService;
import com.jingfu.dto.*;
import com.jingfu.util.CommonResult;
import com.jingfu.util.PageResult;
import com.jingfu.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/1/31 下午12:34
 **/
@Controller
@Slf4j
public class RolerController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;


    /**
     * 角色视图
     *
     * @return
     */
    @RequestMapping("/role-index")
    public String getRoleIndex() {
        return "system/role";
    }

    /**
     * 权限视图
     *
     * @return
     */
    @RequestMapping("/permission-index")
    public String getPermissionIndex() {
        return "system/permission";
    }

    /**
     * 角色列表
     *
     * @param sysRoleDTO
     * @return
     */
    @RequestMapping("/role/list")
    @ResponseBody
    public PageResult<List<SysRoleDTO>> getRoleList(SysRoleDTO sysRoleDTO) {
        PageUtil.startPage();
        List<SysRoleDTO> roleList = roleService.getRoleList(sysRoleDTO);
        return PageUtil.buidPage(roleList);
    }


    /**
     * 角色状态切换
     *
     * @param roleId
     * @param enable
     * @return
     */
    @RequestMapping("/role/enable/{roleId}/{enable}")
    public CommonResult userEnable(@PathVariable String roleId, @PathVariable String enable) {
        roleService.roleEnable(roleId, enable);
        return CommonResult.ok().build();
    }

    /**
     * 角色新增视图
     *
     * @return
     */
    @RequestMapping("/role-add")
    public String getRoleAddView() {
        return "system/operate/roleAdd";
    }

    /**
     * 角色编辑
     *
     * @return
     */
    @RequestMapping("/role-edit")
    public String getRoleEditView() {
        return "system/operate/roleEdit";
    }

    /**
     * 角色新增
     *
     * @param sysRoleDTO
     * @return
     */
    @RequestMapping("/role/create")
    @ResponseBody
    public CommonResult createOrUpdateRole(@RequestBody SysRoleDTO sysRoleDTO) {
        log.info("角色新增，请求参数：【{}】", sysRoleDTO);
        roleService.createOrUpdateRole(sysRoleDTO);
        return CommonResult.ok().build();
    }

    /**
     * 角色删除
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/role/delete/{roleId}")
    @ResponseBody
    public CommonResult deleteRole(@PathVariable String roleId) {
        log.info("请求删除角色，角色Id: {}", roleId);
        roleService.deleteRole(roleId);
        return CommonResult.ok().build();
    }

    /**
     * 角色批量删除
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/role/batch-remove/{roleId}")
    @ResponseBody
    public CommonResult batchDeleteRole(@PathVariable String roleId) {
        log.info("请求批量删除角色，角色Id:{}", roleId);
        Arrays.stream(roleId.split(",")).forEach(id -> {
            roleService.deleteRole(id);
        });
        return CommonResult.ok().build();
    }

    /**
     * 角色授权视图
     *
     * @return
     */
    @RequestMapping("/role-perm")
    public String getRolePermView() {
        return "system/operate/rolePerm";
    }

    /**
     * 获取所有权限
     *
     * @return
     */
    @RequestMapping("/perm-all")
    @ResponseBody
    public CommonResult<List<SysPowerDTO.MenuDTO>> getPermAll() {
        return CommonResult.ok(userService.getPermAll());
    }

    /**
     * 更加角色id获取权限列表
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/get-role-perms")
    @ResponseBody
    public CommonResult<List<SysPowerDTO>> getRolePermList(@RequestParam(value = "roleId", required = false) String roleId) {
        return CommonResult.ok(roleService.getRolePermList(roleId));
    }

    /**
     * 绑定角色&权限关系
     *
     * @param rolePermDTO
     * @return
     */
    @RequestMapping("/create/role-perm")
    @ResponseBody
    public CommonResult createRolePerm(@RequestBody RolePermDTO rolePermDTO) {
        roleService.createRolePerm(rolePermDTO);
        return CommonResult.ok().build();
    }


    /**
     * 菜单权限列表
     *
     * @param powerDTO
     * @return
     */
    @RequestMapping("/get/perm-list")
    @ResponseBody
    public CommonResult<List<PowerDTO>> getPermList(PowerDTO powerDTO) {
        PageUtil.startPage();
        List<PowerDTO> powerList = roleService.getPermList(powerDTO);
        return PageUtil.buidPage(powerList);
    }

    /**
     * 权限状态切换
     *
     * @param powerId
     * @param enable
     * @return
     */
    @RequestMapping("/perm/enable/{powerId}/{enable}")
    @ResponseBody
    public CommonResult permEnable(@PathVariable String powerId, @PathVariable String enable) {
        roleService.permEnable(powerId, enable);
        return CommonResult.ok().build();
    }

    /**
     * 权限删除-逻辑删
     *
     * @param powerId
     * @return
     */
    @RequestMapping("/perm/delete/{powerId}")
    @ResponseBody
    public CommonResult deletePermission(@PathVariable String powerId) {
        log.info("请求删除权限，权限Id: {}", powerId);
        roleService.deletePermission(powerId);
        return CommonResult.ok().build();
    }

    /**
     * 权限批量删除
     *
     * @param powerId
     * @return
     */
    @RequestMapping("/perm/batch-remove/{powerId}")
    @ResponseBody
    public CommonResult batchDeletePerm(@PathVariable String powerId) {
        log.info("请求批量删除角色，角色Id:{}", powerId);
        Arrays.stream(powerId.split(",")).forEach(id -> {
            roleService.deletePermission(id);
        });
        return CommonResult.ok().build();
    }

    /**
     * 权限新增视图
     * @return
     */
    @RequestMapping("/perm-add-view")
    public String permAddView() {
        return "system/operate/permAdd";
    }

    /**
     * 权限修改视图
     * @return
     */
    @RequestMapping("/perm-edit-view")
    public String permEditView() {
        return "system/operate/permEdit";
    }

    /**
     * 权限新增
     * @param powerDTO
     * @return
     */
    @RequestMapping("/permission/create")
    @ResponseBody
    public CommonResult permissionCreate(@RequestBody PowerDTO powerDTO) {
        roleService.permissionCreateOrUpdate(powerDTO);
        return CommonResult.ok().build();
    }
}
